package academic.concordia.bankofquebec.managedbean;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import academic.concordia.bankofquebec.bean.bankAccount.IBankAccountServices;
import academic.concordia.bankofquebec.bean.gic.IGICBean;
import academic.concordia.bankofquebec.bean.session.SessionData;
import academic.concordia.bankofquebec.entity.AccountAttributes;
import academic.concordia.bankofquebec.entity.CheckingsAccount;
import academic.concordia.bankofquebec.entity.Client;
import academic.concordia.bankofquebec.entity.GIC;
import academic.concordia.bankofquebec.entity.SavingsAccount;

@ManagedBean(name="gicOpeningManagedBean")
@SessionScoped
public class GICOpeningManagedBean implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private GIC gic;
	
	@ManagedProperty(value="#{SessionData}")
	private SessionData sessionData;
	
	@ManagedProperty(value="#{GICBean}")
	private IGICBean gicBean;

	@PostConstruct
	void initialize(){
		this.gic = new GIC();
	}
	
	public String openAccount(){
		Client client = sessionData.getClient();
		if (!gicBean.createGIC(client, gic)){
			FacesContext facesContext = FacesContext.getCurrentInstance();
			FacesMessage facesMessage = new FacesMessage("Client already have this product");
			facesContext.addMessage(null, facesMessage);
			return "";
		}
		
		
		CheckingsAccount checkingsAccount = (CheckingsAccount) client.getProduct(AccountAttributes.CHECKINGS_ACCOUNT.name());
		gicBean.transferInitialInvestment(gic, checkingsAccount);
		
		return "products";
	}

	public GIC getGic() {
		return gic;
	}

	public void setGic(GIC gic) {
		this.gic = gic;
	}

	public SessionData getSessionData() {
		return sessionData;
	}

	public void setSessionData(SessionData sessionData) {
		this.sessionData = sessionData;
	}

	public IGICBean getGicBean() {
		return gicBean;
	}

	public void setGicBean(IGICBean gicBean) {
		this.gicBean = gicBean;
	}


}
